package com.hao.tool.extra.poi.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * Excel表格行样式
 * Created by haol on 2017/5/13.
 */
public class ExcelRow {
    private ExcelCellStyle cellStyle;

    public ExcelRow(HSSFWorkbook workbook) {
        cellStyle = new ExcelCellStyle(workbook);
    }


    public HSSFSheet createRowHead(HSSFSheet hssfSheet, String[] head, int row) {
        HSSFRow hssfRow = hssfSheet.createRow(row);
        int cellPlus = 0;
        this.createSignHead(hssfSheet, hssfRow, head, cellPlus);
        return hssfSheet;
    }

    public HSSFSheet createSignHead(HSSFSheet hssfSheet, HSSFRow hssfRow, String[] head, int cellPlus) {
        SetValidation validation = new SetValidation();
        HSSFCell hssfCell;
        for (int i = 0; i < head.length; i++) {
            hssfCell = hssfRow.createCell(i + cellPlus);
            hssfCell.setCellValue(new HSSFRichTextString(head[i]));
            hssfCell.setCellStyle(cellStyle.alignStyle());
            //增加锁，不允许更改数据
            validation.setDataValidation(hssfSheet, hssfCell);
        }
        return hssfSheet;
    }

    public HSSFSheet createRowHead(HSSFSheet hssfSheet, String[] head, int rownum, int depSize) {
        SetValidation validation = new SetValidation();
        HSSFCell hssfCell;
        HSSFRow hssfRow = hssfSheet.createRow(rownum);
        int cellPlus = 0;
        for (; depSize > 0; depSize--) {
            this.createSignHead(hssfSheet, hssfRow, head, cellPlus);
            cellPlus = cellPlus + head.length;
        }

        return hssfSheet;
    }

    /**
     * 创建一行
     * @param hssfSheet excel sheet
     * @param rowNum 行数
     * @param textString rowNum行0-lastCol内的文本信息
     * @param lastCol 最后一列数，0为第一列
     * @param height 高度
     * @return
     */
    public HSSFSheet createRow(HSSFSheet hssfSheet, int rowNum, String textString, int lastCol, int height) {
        CellRangeAddress range;
        SetValidation validation = new SetValidation();
        HSSFRow hssfRow = hssfSheet.createRow(rowNum);
        hssfRow.setHeight((short) height);
        HSSFCell hssfCell;
        hssfCell = hssfRow.createCell(0);

        hssfCell.setCellValue(new HSSFRichTextString(textString));


        range = new CellRangeAddress(rowNum, rowNum, 0, lastCol - 1);
        hssfSheet.addMergedRegion(range);

        hssfCell.setCellStyle(cellStyle.alignStyle());
       // validation.setDataValidation(hssfSheet, hssfCell);

        return hssfSheet;
    }
}
